package com.sky.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sky.blog.dao.enums.StatusCode;
import com.sky.blog.dao.mapper.SysUserMapper;
import com.sky.blog.dao.mapper.UserNoticeMapper;
import com.sky.blog.dao.pojo.SysUser;
import com.sky.blog.dao.vo.BlogSiteVo;
import com.sky.blog.service.SysUserService;
import com.sky.blog.util.ApplicationContextUtils;
import com.sky.blog.util.DozerUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author sky  2022/3/26
 * @version 1.0
 */
@Slf4j
@Service
public class SysUserServiceImpl implements SysUserService {

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private UserNoticeMapper userNoticeMapper;

    @Override
    public void addSysUser(SysUser sysUser) {
        sysUserMapper.insert(sysUser);
    }

    @Override
    public BlogSiteVo getBlogSite(Long blogUserId) {
        if (blogUserId == null) {
            return (BlogSiteVo) ApplicationContextUtils.getBeanByName("defaultBlogSite");
        }
        try {
            LambdaQueryWrapper<SysUser> wrapper = Wrappers.lambdaQuery(SysUser.class);
            wrapper.select(SysUser::getAvatar, SysUser::getSlogan)
                    .eq(SysUser::getId, blogUserId);
            SysUser sysUser = sysUserMapper.selectOne(wrapper);
            BlogSiteVo blogSiteVo = DozerUtils.copy(sysUser, BlogSiteVo.class);
            blogSiteVo.setNotices(userNoticeMapper.selectUserNoticesByUserId(blogUserId, StatusCode.NORMAL));
            return blogSiteVo;
        } catch (Exception e) {
            log.warn("没有查到用户的相关信息{}", e.getMessage());
        }
        return (BlogSiteVo) ApplicationContextUtils.getBeanByName("defaultBlogSite");
    }


}
